//
//  thingViewController.m
//  UI_HLJ_Frame
//
//  Created by dlios on 15-7-2.
//  Copyright (c) 2015年 毕宏伟. All rights reserved.
//
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#import "thingViewController.h"
#import "thingCell.h"
#import "AFNetworking.h"
#import "weddingThingModel.h"
#import "thingdetailsViewController.h"
#import "MBProgressHUD.h"

@interface thingViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,click>
@property (nonatomic ,retain)UICollectionView *collectionView;
@property (nonatomic ,retain)NSIndexPath *index;

@property(nonatomic,retain)UISegmentedControl *segmentedControl;
@property(nonatomic,retain)NSMutableArray *HunShaArr;
@property(nonatomic,retain)NSMutableArray *LifuFArr;
@property(nonatomic,retain)NSMutableArray *HunXieArr;
@property(nonatomic,retain)NSMutableArray *PeiShiArr;
@property(nonatomic,retain)NSMutableArray *ShouShiArr;
@property(nonatomic,retain)NSMutableArray *HunQingArr;
@property(nonatomic,retain)NSMutableArray *JiaJuArr;
@property(nonatomic,retain)MBProgressHUD *hud;


@end

@implementation thingViewController

- (void)dealloc
{
    [_collectionView  release];
    [_segmentedControl release];
    [_index release];
    [_HunQingArr release];
    [_HunShaArr release];
    [_HunXieArr release];
    [_JiaJuArr release];
    [_PeiShiArr release];
    [_ShouShiArr release];
    [super dealloc];
}



- (void)viewDidLoad {
    [super viewDidLoad];
   //创建分页按钮并赋值
    self.view.backgroundColor = [UIColor colorWithRed:0.996 green:1.000 blue:0.972 alpha:1.000];
    NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"婚纱",@"礼服",@"婚鞋",@"配饰",@"首饰",@"婚庆",@"家居",nil];
   
    self.segmentedControl= [[UISegmentedControl alloc]initWithItems:segmentedArray];
    
    
    self.segmentedControl.frame=CGRectMake(0, 0, self.view.frame.size.width, 30);
    [self.segmentedControl addTarget:self action:@selector(segmentAction:)forControlEvents:UIControlEventValueChanged];
    self.segmentedControl.tintColor=[UIColor colorWithRed:1.000 green:0.404 blue:0.651 alpha:1.000];
//    self.segmentedControl.layer.masksToBounds=NO;
    [self.view addSubview:self.segmentedControl];
    [_segmentedControl release];
    [self creatData];
    [self creatCollectionView];
  
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.view addSubview:self.hud];
    self.hud.labelText = @"loading...";
    // Do any additional setup after loading the view.
}


- (void)creatCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    flowLayout.itemSize = CGSizeMake(WIDTH, HEIGHT);
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;//最小行距
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 30, WIDTH, HEIGHT) collectionViewLayout:flowLayout];
    self.collectionView.pagingEnabled = YES;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView setBackgroundColor:[UIColor colorWithRed:0.086 green:1.000 blue:0.038 alpha:0.000]];
    
    // 注册
    [self.collectionView registerClass:[thingCell class] forCellWithReuseIdentifier:@"thingcell"];
    [self.view addSubview:self.collectionView];
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 7;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.index =indexPath;
    
    thingCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"thingcell" forIndexPath:indexPath];
    
   //判断是第几个collectionview 设置点击方法的代理 并设置和分页按钮保持同步
    if (self.index.row==0) {
        cell.flag = indexPath.row;
        cell.hunshaarr = self.HunShaArr;
        cell.delegatey=self;
         self.segmentedControl.selectedSegmentIndex = self.index.row;
    }else if(self.index.row == 1){
        cell.flag = indexPath.row;
        cell.lifuarr = self.LifuFArr;
        cell.delegatey=self;
        self.segmentedControl.selectedSegmentIndex = self.index.row;

    }else if(self.index.row == 2){
        cell.flag = indexPath.row;
        cell.hunxiearr = self.HunXieArr;
        cell.delegatey=self;
 self.segmentedControl.selectedSegmentIndex = self.index.row;
    }else if(self.index.row== 3){
        cell.flag = indexPath.row;
        cell.peishiarr = self.PeiShiArr;
         self.segmentedControl.selectedSegmentIndex = self.index.row;
    }else if(self.index.row == 4){
        cell.flag = indexPath.row;
        cell.shoushiarr  = self.ShouShiArr;
         self.segmentedControl.selectedSegmentIndex = self.index.row;
    }else if(self.index.row == 5){
        cell.flag = indexPath.row;
        cell.hunqingarr = self.HunQingArr;
         self.segmentedControl.selectedSegmentIndex = self.index.row;
    }else if(self.index.row == 6){
        cell.flag = indexPath.row;
        cell.jujiaarr = self.JiaJuArr;
         self.segmentedControl.selectedSegmentIndex = self.index.row;
    }
    
    

    return cell;
}


//数据解析

- (void)creatData
{
    
    self.HunShaArr = [NSMutableArray array];
    self.HunQingArr = [NSMutableArray array];
    self.HunXieArr = [NSMutableArray array];
    self.JiaJuArr = [NSMutableArray array];
    self.LifuFArr = [NSMutableArray array];
    self.ShouShiArr = [NSMutableArray array];
    self.PeiShiArr = [NSMutableArray array];
    
    AFHTTPRequestOperationManager *manger = [[AFHTTPRequestOperationManager alloc]init];
    for (NSInteger i= 1; i<8; i++) {
        NSString *url = [NSString stringWithFormat:@"http://www.hunliji.com/products.json?category=%ld&sort=0&page=1",i];
    
    [manger GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
      
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr = dic[@"products"];
        for (NSDictionary *tempdic in arr) {
            weddingThingModel *thing= [[weddingThingModel alloc]init];
            [thing setValuesForKeysWithDictionary:tempdic];
     // 不同数据 放到不同的数组里
            switch (i) {
                case 1:
                    [self.HunShaArr addObject:thing];
                    break;
                case 2:
                    [self.LifuFArr addObject:thing];
                    break;
                case 3:
                    [self.HunXieArr addObject:thing];
                    break;
                case 4:
                    [self.PeiShiArr addObject:thing];
                    break;
                case 5:
                    [self.ShouShiArr addObject:thing];
                    break;
                case 6:
                    [self.HunQingArr addObject:thing];
                    break;
                case 7:
                    [self.JiaJuArr addObject:thing];
                    break;
                default:
                    break;
            }
            [thing release];
        }
          [self.collectionView reloadData];
        [self.hud hide:YES afterDelay:1];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    }];
    }
    
}





-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath model:(weddingThingModel *)model{
    thingdetailsViewController *thingVC = [[thingdetailsViewController alloc]init];
    [self.navigationController pushViewController:thingVC animated:YES];
    thingVC.bid=model.bid;
   
}



-(void)segmentAction:(UISegmentedControl *)Seg{
    
    NSInteger Index = Seg.selectedSegmentIndex;
    // 设置分页按钮 点击方法  设置collectionvew 的偏移量
    switch (Index) {
            
        case 0:
            self.collectionView.contentOffset=CGPointMake(self.view.frame.size.width*Index, 0);
     
            [self.collectionView reloadData];
            break;
            
        case 1:
              self.collectionView.contentOffset=CGPointMake(self.view.frame.size.width*Index, 0);
          
              [self.collectionView reloadData];
            break;
            
        case 2:
            self.collectionView.contentOffset=CGPointMake(self.view.frame.size.width*Index, 0);

            [self.collectionView reloadData];
      
            break;
            
        case 3:
            self.collectionView.contentOffset=CGPointMake(self.view.frame.size.width*Index, 0);
   
            [self.collectionView reloadData];
            
            break;
            
        case 4:
            self.collectionView.contentOffset=CGPointMake(self.view.frame.size.width*Index, 0);
            
            [self.collectionView reloadData];
            
            break;
            
        case 5:
            self.collectionView.contentOffset=CGPointMake(self.view.frame.size.width*Index, 0);
   
            [self.collectionView reloadData];

            break;    
            
        default: self.collectionView.contentOffset=CGPointMake(self.view.frame.size.width*Index, 0);
      
            [self.collectionView reloadData];
            
            break;
            
    }
    
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
